<template>
  <div style="width: 100%; height: 100%;">
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="50"></el-table-column>
      <el-table-column prop="username" label="用户名" width="180"></el-table-column>
      <el-table-column prop="realname" label="姓名" width="180"></el-table-column>
      <el-table-column prop="department" label="部门" width="180"></el-table-column>
      <el-table-column prop="phone" label="手机" width="180"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
      <el-table-column prop="role" label="角色" width="180"></el-table-column>
      <el-table-column fixed="right" label="操作" width="160">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="text" size="small">修改密码</el-button>
          <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 修改密码对话框 -->
    <el-dialog title="修改密码" :visible.sync="editDialogVisible" width="30%">
      <el-form :model="editForm" label-width="120px">
        <el-form-item label="用户名">
          <el-input v-model="editForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="editForm.newPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input v-model="editForm.confirmPassword" type="password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "HomeView",
  data() {
    return {
      tableData: [
        { id: 1, username: 'root', realname: '管理员', department: '管理员', phone: '000000000001', email: '000002@qq.com', role: 'administrator' },
        { id: 2, username: '000001', realname: '一号员工', department: '某某部门', phone: '000000000001', email: '000002@qq.com', role: 'administrator' },
        { id: 3, username: '000002', realname: '二号员工', department: '某某部门', phone: '000000000001', email: '000002@qq.com', role: 'user' },
        { id: 4, username: '000003', realname: '三号员工', department: '某某部门', phone: '000000000001', email: '000003@qq.com', role: 'user' },
        { id: 5, username: '000004', realname: '四号员工', department: '某某部门', phone: '000000000001', email: '000004@qq.com', role: 'user' }
      ],
      editDialogVisible: false,
      editForm: {
        username: '',
        newPassword: '',
        confirmPassword: ''
      }
    };
  },
  methods: {
    handleEdit(row) {
      this.editForm.username = row.username;
      this.editDialogVisible = true;
    },
    confirmEdit() {
      if (this.editForm.newPassword !== this.editForm.confirmPassword) {
        this.$message.error('两次输入的密码不一致');
        return;
      }
      // 这里可以调用后端接口进行密码修改
      console.log('修改密码:', this.editForm);
      this.editDialogVisible = false;
      this.editForm.newPassword = '';
      this.editForm.confirmPassword = '';
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 这里可以调用后端接口进行删除操作
        const index = this.tableData.findIndex(item => item.id === row.id);
        if (index !== -1) {
          this.tableData.splice(index, 1);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    }
  }
};
</script>

<style scoped>
.el-table .cell {
  white-space: normal;
}
</style>